package com.founder.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.founder.model.User;
import com.founder.service.IUserService;

public class CustomRealm extends AuthorizingRealm {

	@Autowired
	private IUserService userService;

	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		// UsernamePasswordToken token2 = (UsernamePasswordToken) token;

		// 从token中 获取用户身份信息
		// UsernamePasswordToken token2 = (UsernamePasswordToken) token;  
		String username = (String) token.getPrincipal();
		 
		User user = userService.getUser(username);
		// 拿username从数据库中查询
		// ....
		if (user != null) {
			return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), user.getName());
		} else {
			return null;
		}

	}
}